<template>
  <div class="container">
    <div class="form-login">
      <div class="form-title">
        <div class="title1">
          欢迎
        </div>
        <div class="title2">
          登陆
        </div>
      </div>
      <div class="form-group">
        <label for="username" class="input-title">用户名:</label>
        <input type="text" class="form-control" v-model="username" id="username" placeholder="Enter username">
      </div>
      <div class="form-group">
        <label for="pwd" class="input-title">密码:</label>
        <input type="password" class="form-control" v-model="password" id="pwd" placeholder="Enter password">
      </div>
      <button @click="login" class="btn btn-success btn-login">登录</button>
      <button @click="goRegister" class="btn btn-info btn-register">现在去注册</button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'loginPage',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    login () {
      if (this.username === '' || this.password === '') {
        alert('用户名或密码未输入!')
      } else {
        this.$http.post('/java_kunpeng/login', {
          username: this.username,
          password: this.password
        })
          .then((response) => {
            const json = response.data
            console.log(json)
            if (json.state === '0') {
              const info = json.info
              this.$store.dispatch('login', info)
              console.log(info)
              this.$router.push('/homePage')
            } else {
              alert(json.msg)
            }
          })
          .catch((error) => {
            console.log(error)
          })
      }
    },
    goRegister () {
      this.$router.push('/registerPage')
    }
  }
}
</script>

<style scoped>
  .form-login {
    width: 60%;
    margin: 40px auto;
  }
  .form-title {
    font-size: 35px;
    font-weight: bolder;
    text-align: center;
    margin-bottom: 30px;
  }
  .title1 {
    display: inline;
  }
  .title2 {
    color: cadetblue;
    display: inline;
  }
  .input-title{
    font-size: 20px;
    font-weight: bold;
  }
  .btn-login {
    width: 43%;
    margin-top: 25px;
  }
  .btn-register {
    width: 43%;
    margin-left: 12%;
    margin-top: 25px;
  }
</style>
